package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.HxDayReportView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.system.domain.HxDayReport;
import com.ruoyi.system.domain.HxProject;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.HxDayReportMapper;
import com.ruoyi.system.service.IHxDayReportService;
import com.ruoyi.system.service.IHxProjectService;
import com.ruoyi.system.service.ISysDictDataService;

@Service
public class HxDayReportServiceImpl implements IHxDayReportService {
    @Autowired
    private HxDayReportMapper hxDayReportMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IHxProjectService hxProjectService;

    /**
     * 查询日报信息
     *
     * @param id 日报ID
     * @return 日报信息
     */
    @Override
    public HxDayReport selectHxDayReportById(Long id) {
        return hxDayReportMapper.selectHxDayReportById(id);
    }

    /**
     * 查询日报列表
     *
     * @param hxDayReport 日报信息
     * @return 日报集合
     */
    @Override
    public List<HxDayReport> selectHxDayReportList(HxDayReport hxDayReport) {
        return hxDayReportMapper.selectHxDayReportList(hxDayReport);
    }

    /**
     * 新增日报
     *
     * @param hxDayReport 日报信息
     * @return 结果
     */
    @Override
    public int insertHxDayReport(HxDayReport hxDayReport) {
        return hxDayReportMapper.insertHxDayReport(hxDayReport);
    }

    /**
     * 修改日报
     *
     * @param hxDayReport 日报信息
     * @return 结果
     */
    @Override
    public int updateHxDayReport(HxDayReport hxDayReport) {
        return hxDayReportMapper.updateHxDayReport(hxDayReport);
    }

    /**
     * 删除日报对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHxDayReportByIds(String ids) {
        return hxDayReportMapper.deleteHxDayReportByIds(Convert.toStrArray(ids));
    }

    @Override
    public Map<String, Object> getMonthCal(String month) {
        Map<String, Object> res = new HashMap<String, Object>();
        String monthArr[] = month.split("-");
        String lastDay = monthArr[0] + "-" + monthArr[1] + "-31";

        //1.查询报表 表头 ,按研发项目> 综合项目>其他项目排列
        HxProject hxProject = new HxProject();
        hxProject.setType(1);
        List<HxProject> listProject = hxProjectService.selectHxProjectList1(hxProject);
        res.put("yfxm", listProject);
        hxProject.setType(2);
        List<HxProject> listProject1 = hxProjectService.selectHxProjectList1(hxProject);
        res.put("zhxm", listProject1);
        hxProject.setType(3);
        List<HxProject> listProject2 = hxProjectService.selectHxProjectList1(hxProject);
        res.put("qtxm", listProject2);

        List<HxDayReport> list = hxDayReportMapper.selectMonthReport(month, lastDay);
        List<Long> userList = new ArrayList<>();
        List<HxDayReportView> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (!userList.contains(list.get(i).getUserId())) {
                userList.add(list.get(i).getUserId());
            }
        }
        for (int j = 0; j < userList.size(); j++) {
            HxDayReportView hx = new HxDayReportView();
            Map<String, Object> map = new HashMap<>();
            Double total_work_time=0.0;
            for (int i = 0; i < list.size(); i++) {
                if (userList.get(j).equals(list.get(i).getUserId())) {
                    hx.setUserId(list.get(i).getUserId());
                    hx.setAncestors(list.get(i).getAncestors());
                    hx.setDeptName(list.get(i).getDeptName());
                    hx.setPosition(list.get(i).getPosition());
                    hx.setUserName(list.get(i).getUserName());
                    hx.setGsName(list.get(i).getGsName());
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(1))) {
                    	hx.setOther_pro1(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(2))) {
                    	hx.setOther_pro2(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(3))) {
                    	hx.setOther_pro3(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(4))) {
                    	hx.setOther_pro4(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(5))) {
                    	hx.setOther_pro5(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(6))) {
                    	hx.setOther_pro6(list.get(i).getTotalTime());
                    }
                    if(list.get(i).getProjectId()!=null&&list.get(i).getProjectId().equals(new Long(7))) {
                    	hx.setOther_pro7(list.get(i).getTotalTime());
                    }
                    
                    hx.setRemark("");
                    total_work_time=total_work_time+list.get(i).getTotalTime();
					map.put(list.get(i).getProname(),list.get(i).getTotalTime());
                }
            }
           // total_work_time=total_work_time+hx.getOther_pro1()+hx.getOther_pro2()+hx.getOther_pro3()+hx.getOther_pro4()+hx.getOther_pro5()+hx.getOther_pro6();
            map.put("total_work_time",total_work_time);
			hx.setMap(map);
			list1.add(hx);
        }
        res.put("list", list1);
        String htmlRow = "";
        Long lastUserId = null;
/*		for(int i=0;i<list.size();i++) {
			if(i!=0) {
				Long userId = list.get(i).getUserId();
				if(lastUserId == userId) {
					htmlRow +="";
				}
			}
			
			Long projectId = list.get(i).getProjectId();
		}*/

        return res;
    }

    @Override
    public List<HxDayReport> selectTodayReportList(HxDayReport hxDayReport) {
        return hxDayReportMapper.selectTodayReportList(hxDayReport);
    }

	@Override
	public List<HxDayReport> selectDayReport(HxDayReport hxDayReport) {
		  return hxDayReportMapper.selectDayReport(hxDayReport);
	}

}
